package com.hdey.controller.chao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdey.mapper.chao.ShenlingMapper;
import com.hdey.pojo.chao.Chuku;
import com.hdey.pojo.chao.Chukuzi;
import com.hdey.pojo.chao.Shenling;
import com.hdey.pojo.chao.Shenlingzi;
import com.hdey.service.chao.ChukuService;
import com.hdey.service.chao.ChukuziService;
import com.hdey.service.chao.ShenlingService;
import com.hdey.service.chao.ShenlingziService;
import com.hdey.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Random;

@RestController
@RequestMapping("/sl")
@Slf4j
public class ShenlingController {

        @Resource
        private ShenlingService shenlingService;

        @Resource
        private ShenlingMapper shenlingMapper;

        @Resource
        private ShenlingziService shenlingziService;

        @Resource
        private ChukuService chukuService;

        @Resource
        private ChukuziService chukuziService;


        private String msg;

        @GetMapping("/index")
        public ModelAndView index(){

            return new ModelAndView("chao/shenling");

        }
//        // 添加
//        @RequestMapping("/add")
//        public Result add(Wuzi wuzi) {
//                System.out.println(wuzi);
//                wuzi.setCreateDate(LocalDate.now());
//                boolean flag = wuziService.save(wuzi);
//                return flag ? Result.success("添加成功") : Result.fail("添加失败");
//        }

        @RequestMapping("/search")
        @ResponseBody
        public Result search(Integer page, Integer limit, String applyMasterNo){
                Page<Shenling> shenlingPage = shenlingService.searchPage(page, limit, applyMasterNo);
                System.out.println(shenlingPage.getRecords());
                return Result.success(shenlingPage);
        }

        // 添加
        @RequestMapping("/add")
        public Result add(Shenling shenling, Shenlingzi shenlingzi, Chuku chuku, Chukuzi chukuzi) {
                System.out.println(shenling);
                System.out.println(shenlingzi);
                System.out.println(chuku);
                Random random=new Random();
                String a = String.valueOf((Math.random() * 9 + 1) * 10000);
                String numStr = a.toString().substring(0, 5);
                String numStr1 = a.toString().substring(0, 4);
                System.out.println(a);
                shenling.setApplyMasterNo(numStr);//订单号
                shenlingzi.setApplyMasterNo(numStr);//订单号
                chuku.setApplyMasterNo(numStr);//出库表申领单号
                chuku.setOutstorageMasterNo(numStr1);//出库单号
                chukuzi.setOutstorageMasterNo(numStr1);//出库子表出库单号
                shenling.setCreateDate(LocalDate.now());//日期
                shenlingzi.setCreateDate(LocalDate.now());
                chukuzi.setCreateDate(LocalDate.now());
                chuku.setCreateDate(LocalDate.now());
                boolean flag = shenlingService.save(shenling);
                boolean flag1=shenlingziService.save(shenlingzi);
                boolean flag2=chukuService.save(chuku);
                boolean flag3=chukuziService.save(chukuzi);
                return flag ? Result.success("添加成功") : Result.failed("添加失败");

        }


        // 删除
        @RequestMapping("/del")
        public Result del(Integer applyMasterId){

                LambdaQueryWrapper<Shenling> wrapper=new LambdaQueryWrapper<>();
                wrapper.eq(Shenling::getApplyMasterId,applyMasterId);
                int  flag = shenlingMapper.delete(wrapper);
//                String msg = "";
//
//                if(flag){
//                        msg = "删除成功!";
//                }else {
//                        msg = "删除失败";
//                }
//                return Result.fail(msg);
                return flag > 0 ? Result.success("删除成功") : Result.failed("删除失败");
        }


        @PostMapping("/update")
        public Result update(Integer applyMasterId){
                log.error(applyMasterId.toString());
                System.out.println(applyMasterId);
                log.error("dfs");
                try {
                        shenlingService.update(applyMasterId);
                        msg="申领单处理成功";
                        return Result.success(msg);
                }catch (Exception ex){
                        msg="申领单处理失败";
                        log.error(msg,ex);
                        return Result.failed(msg);
                }

        }


}
